package com.xinchuang.service.impl;

import com.xinchuang.domain.vo.byd.BYDQuotationDetailVo;
import com.xinchuang.domain.vo.byd.BYDQuotationSubDetailVo;
import com.xinchuang.domain.vo.byd.BYDQuotationVo;
import com.xinchuang.domain.vo.df.DfDetailData;
import com.xinchuang.domain.vo.df.DfObject;
import com.xinchuang.domain.vo.df.DfQuotationVo;
import com.xinchuang.domain.vo.out.req.NormalOutQuotationDetailReqVo;
import com.xinchuang.domain.vo.out.req.NormalOutQuotationReqVo;
import com.xinchuang.domain.vo.qr.QrQuotationDetailVo;
import com.xinchuang.domain.vo.qr.QrQuotationSubDetailVo;
import com.xinchuang.domain.vo.qr.QrQuotationVo;
import com.xinchuang.service.CheckQuotationDataService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class CheckQuotationDataServiceImpl implements CheckQuotationDataService {
    @Override
    public void checkDfData(DfQuotationVo dfQuotationVo) {

        for (DfObject dfObject : dfQuotationVo.getDataList()) {
            DfDetailData detailData = dfObject.getDetailData();
            //detailData.getMachiningMaterialCost().checkMachiningMaterialCost();
            if(null != detailData.getHeatTreatmentCost()){
                detailData.getHeatTreatmentCost().checkHeatTreatmentCost();
            }
            if(null != detailData.getAssemlyMaterialCost()){
                detailData.getAssemlyMaterialCost().checkAssemlyMaterialCost();
            }
            if(null != detailData.getDesignCost()){
                detailData.getDesignCost().checkDesignCost();
            }
            if(null != detailData.getManufacturingCost()){
                detailData.getManufacturingCost().checkManufacturingCost();
            }
            if(null != detailData.getCMMMeasure()){
                detailData.getCMMMeasure().checkCMMMeasure();
            }


        }
    }

    @Override
    public void checkQrData(QrQuotationVo qrQuotationReqVo) {
        for (QrQuotationDetailVo detailVo : qrQuotationReqVo.getDataList()) {
            QrQuotationSubDetailVo detailData = detailVo.getDetailData();
            //加工材料费
            if(null != detailData.getMachiningMaterialFee()){
                detailData.getMachiningMaterialFee().checkMachiningMaterialCost();
            }
            //热处理(费用)
            if(null != detailData.getHeatTreatmentFee()){
                detailData.getHeatTreatmentFee().checkHeatTreatmentCost();
            }
            //装配材料费
            if(null != detailData.getAssemlyMaterialFee()){
                detailData.getAssemlyMaterialFee().checkAssemlyMaterialCost();
            }
            //设计费
            if(null != detailData.getDesignFee()){
                detailData.getDesignFee().checkDesignCost();
            }
            //加工费
            if(null != detailData.getManufacturingFee()){
                detailData.getManufacturingFee().checkManufacturingCost();
            }
            //三坐标测量费
            if(null != detailData.getCMMMeasure()){
                detailData.getCMMMeasure().checkCMMMeasure();
            }

        }
    }

    @Override
    public void checkBYDData(BYDQuotationVo bydQuotationVo) {
        for (BYDQuotationDetailVo detailVo : bydQuotationVo.getDataList()) {
            BYDQuotationSubDetailVo detailData = detailVo.getDetailData();
            //制造成本
            if(null != detailData.getManufacturingFee()){
                detailData.getManufacturingFee().checkFee();
            }

            //营业费用
            if(null != detailData.getOperatingFee()){
                detailData.getOperatingFee().checkFee();
            }

            //其他辅件
            if(null != detailData.getOtherAccessoriesFee()){
                detailData.getOtherAccessoriesFee().checkFee();
            }
            //钢材成本
            if(null != detailData.getSteelFee()){
                detailData.getSteelFee().checkFee();
            }


        }
    }


    /**
     * 校验通用报价数据
     * 异常信息为:"xxx计算结果不一致。结果为xxx"
     *
     * @param normalOutQuotationReqVo
     */
    @Override
    public void checkNormalData(NormalOutQuotationReqVo normalOutQuotationReqVo) {
        if (Objects.nonNull(normalOutQuotationReqVo)) {
            List<NormalOutQuotationDetailReqVo> normalOutQuotationDetailReqVoList = normalOutQuotationReqVo.getDataList();
            if (Objects.nonNull(normalOutQuotationDetailReqVoList) && !normalOutQuotationDetailReqVoList.isEmpty()) {
                normalOutQuotationDetailReqVoList.stream()
                        .filter(normalOutQuotationDetailReqVo -> Objects.nonNull(normalOutQuotationDetailReqVo.getDetailData()))
                        .allMatch(normalOutQuotationDetailReqVo -> {
                            if (Objects.nonNull(normalOutQuotationDetailReqVo.getDetailData().getMachiningMaterialCost())) {
                                normalOutQuotationDetailReqVo.getDetailData().getMachiningMaterialCost().checkIsEqual();
                            }
                            if (Objects.nonNull(normalOutQuotationDetailReqVo.getDetailData().getHeatTreatmentCost())) {
                                normalOutQuotationDetailReqVo.getDetailData().getHeatTreatmentCost().checkIsEqual();
                            }
                            if (Objects.nonNull(normalOutQuotationDetailReqVo.getDetailData().getAssemlyMaterialCost())) {
                                normalOutQuotationDetailReqVo.getDetailData().getAssemlyMaterialCost().checkIsEqual();
                            }
                            if (Objects.nonNull(normalOutQuotationDetailReqVo.getDetailData().getDesignFee())) {
                                normalOutQuotationDetailReqVo.getDetailData().getDesignFee().checkIsEqual();
                            }
                            if (Objects.nonNull(normalOutQuotationDetailReqVo.getDetailData().getManufacturingCost())) {
                                normalOutQuotationDetailReqVo.getDetailData().getManufacturingCost().checkIsEqual();
                            }
                            if (Objects.nonNull(normalOutQuotationDetailReqVo.getDetailData().getCmmmeasure())) {
                                normalOutQuotationDetailReqVo.getDetailData().getCmmmeasure().checkIsEqual();
                            }
                            return true;
                        });

                normalOutQuotationDetailReqVoList.stream().forEach(normalOutQuotationDetailReqVo -> {
                    List<BigDecimal> resultList = new ArrayList<>();
                    if (Objects.nonNull(normalOutQuotationDetailReqVo.getDetailData().getMachiningMaterialCost())) {
                        resultList.add(BigDecimal.valueOf(normalOutQuotationDetailReqVo.getDetailData().getMachiningMaterialCost().getSubTotal()));
                    }
                    if (Objects.nonNull(normalOutQuotationDetailReqVo.getDetailData().getHeatTreatmentCost())) {
                        resultList.add(BigDecimal.valueOf(normalOutQuotationDetailReqVo.getDetailData().getHeatTreatmentCost().getSubTotal()));
                    }
                    if (Objects.nonNull(normalOutQuotationDetailReqVo.getDetailData().getAssemlyMaterialCost())) {
                        resultList.add(BigDecimal.valueOf(normalOutQuotationDetailReqVo.getDetailData().getAssemlyMaterialCost().getSubTotal()));
                    }
                    if (Objects.nonNull(normalOutQuotationDetailReqVo.getDetailData().getDesignFee())) {
                        resultList.add(BigDecimal.valueOf(normalOutQuotationDetailReqVo.getDetailData().getDesignFee().getSubTotal()));
                    }
                    if (Objects.nonNull(normalOutQuotationDetailReqVo.getDetailData().getManufacturingCost())) {
                        resultList.add(BigDecimal.valueOf(normalOutQuotationDetailReqVo.getDetailData().getManufacturingCost().getSubTotal()));
                    }
                    if (Objects.nonNull(normalOutQuotationDetailReqVo.getDetailData().getCmmmeasure())) {
                        resultList.add(BigDecimal.valueOf(normalOutQuotationDetailReqVo.getDetailData().getCmmmeasure().getSubTotal()));
                    }
                    normalOutQuotationDetailReqVo.getDetailData().getOtherFee().checkIsEqual(resultList.stream().reduce(BigDecimal.ZERO, BigDecimal::add));
                });
            }
        }
    }

}
